package com.jld.managerialsystemforcovid19.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jld.managerialsystemforcovid19.dto.HealthClockDto;
import com.jld.managerialsystemforcovid19.entity.Covid19Data;
import com.jld.managerialsystemforcovid19.entity.HealthClock;
import com.jld.managerialsystemforcovid19.service.HealthClockService;
import com.jld.managerialsystemforcovid19.vo.DataView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HealthClockController {
    @Autowired
    private HealthClockService healthClockService;

    @RequestMapping("/toHealthClock")
    public String toHealthClock() {
        return "admin/healthclock";
    }

    /**
     * 查询所有打卡记录
     *
     * @param healthClockDto
     * @return
     */
    @RequestMapping("/listHealthClock")
    @ResponseBody
    public DataView listHealthClock(HealthClockDto healthClockDto) {
        IPage<HealthClock> page = new Page<>(healthClockDto.getPage(), healthClockDto.getLimit());
        LambdaQueryWrapper<HealthClock> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //姓名模糊查询
        lambdaQueryWrapper.like(healthClockDto.getUserName() != null, HealthClock::getUserName, healthClockDto.getUserName());
        //手机号等值查询
        lambdaQueryWrapper.eq(healthClockDto.getPhone() != null, HealthClock::getPhone, healthClockDto.getPhone());
        healthClockService.page(page, lambdaQueryWrapper);
        DataView dataView = new DataView(page.getTotal(), page.getRecords());
        return dataView;
    }

    /**
     * 新增及修改健康打卡记录
     * 根据id进行新增或者修改。有id修改，无id则进行新增
     */
    @RequestMapping("addHealthClock")
    @ResponseBody
    public DataView addOrUpdateHealthClock(HealthClock healthClock) {
        boolean save = healthClockService.saveOrUpdate(healthClock);
        DataView dataView = new DataView();
        if (save) {
            dataView.setCode(200);
            dataView.setMsg("新增数据成功");
            return dataView;
        }
        dataView.setCode(100);
        dataView.setMsg("失败：未能添加数据！");
        return dataView;
    }

    @RequestMapping("/deleteHealthClockById")
    @ResponseBody
    public DataView deleteHealthClockById(Integer id) {
        healthClockService.removeById(id);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("数据删除成功");

        return dataView;
    }
}
